package com.cskaoyan.service;

import com.cskaoyan.bean.GenericData;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.po.MarketComment;
import com.cskaoyan.bean.po.MarketCommentExample;
import com.cskaoyan.bean.po.MarketTopic;
import com.cskaoyan.bean.po.MarketUser;
import com.cskaoyan.bean.wx.comment.WxCommentCountVo;
import com.cskaoyan.bean.wx.comment.WxCommentListDataVo;
import com.cskaoyan.bean.wx.comment.WxCommentListUserInfoVo;
import com.cskaoyan.bean.wx.comment.WxCommentListVo;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    MarketCommentMapper marketCommentMapper;
@Autowired
    MarketUserMapper marketUserMapper;

    @Override
    public GenericData<MarketComment> query(BaseParam param, Integer userId, Integer valueId) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(valueId != null){
            criteria.andValueIdEqualTo(valueId);
        }
        marketCommentExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<MarketComment> marketComments = marketCommentMapper.selectByExample(marketCommentExample);
        PageInfo pageInfo = new PageInfo(marketComments);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return GenericData.data(total,pages,param.getLimit(), param.getPage(), marketComments);
    }

    @Override
    public Integer deleteById(MarketComment comment) {
        comment.setDeleted(true);
        int update = marketCommentMapper.updateByPrimaryKeySelective(comment);
        return update;
    }
/**
 * @Description: 小程序
  * @param
 * @return:
 * @Author: 蒋镇泽
 * @Date: 2022/6/9 20:32
 */
    @Override
    public WxCommentCountVo queryCount(Integer type, Integer valueId) {
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
        criteria.andValueIdEqualTo(valueId);
        byte b= (byte) type.intValue();
        // criteria.andTypeEqualTo(b);

        List<MarketComment> marketComments = marketCommentMapper.selectByExample(marketCommentExample);
        WxCommentCountVo wxCommentCountVo = new WxCommentCountVo();
        wxCommentCountVo.setAllCount(marketComments.size());
        //有图片的
        MarketCommentExample marketCommentExample2 = new MarketCommentExample();
        MarketCommentExample.Criteria criteria2 = marketCommentExample2.createCriteria();
        criteria2.andValueIdEqualTo(valueId);
        // criteria2.andTypeEqualTo(b);
        criteria2.andHasPictureEqualTo(true);

        List<MarketComment> marketComments2 = marketCommentMapper.selectByExample(marketCommentExample2);
        wxCommentCountVo.setHasPicCount(marketComments2.size());

        return wxCommentCountVo;
    }

    @Override
    public WxCommentListVo queryList(Integer type, Integer valueId, Integer limit, Integer page, Integer showType) {
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
        // marketCommentExample.setOrderByClause("id,desc");
        criteria.andValueIdEqualTo(valueId);
        byte b= (byte) type.intValue();
        criteria.andTypeEqualTo(b);
        List<MarketComment> marketComments = new ArrayList<>();
        if(showType==0){
             marketComments = marketCommentMapper.selectByExample(marketCommentExample);
        }else {
            criteria.andHasPictureEqualTo(true);
             marketComments = marketCommentMapper.selectByExample(marketCommentExample);
        }


        PageHelper.startPage(page, limit);
        PageInfo pageInfo = new PageInfo(marketComments);

        // total是总的数据量 → 是否等于users.length?不是 → 指的是如果不分页的情况下最多会查询出来多少条记录
        // 按照上面的查询的查询条件执行的select count(*)
        long total = pageInfo.getTotal();
        double i = Math.ceil(total /limit);
        int pages = (int) i;


        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = null;
        if (subject.isAuthenticated()) {
            primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        }
        String nickname="";
        String avatar="";
        if(primaryPrincipal!=null){
             nickname = primaryPrincipal.getNickname();
             avatar = primaryPrincipal.getAvatar();
        }


        ArrayList<WxCommentListDataVo> listDataVos = new ArrayList<>();
        WxCommentListUserInfoVo wxCommentListUserInfoVo = new WxCommentListUserInfoVo();
        wxCommentListUserInfoVo.setAvatarUrl(avatar);
        wxCommentListUserInfoVo.setNickName(nickname);
        int temp = 1;

        for (MarketComment marketComment : marketComments) {

            int end = limit * page;
            int start = end - limit+1;
            if (temp >= start && temp <= end) {
                WxCommentListDataVo wxCommentListDataVo = new WxCommentListDataVo();
                wxCommentListDataVo.setUserInfo(wxCommentListUserInfoVo);
                wxCommentListDataVo.setAddTime(marketComment.getAddTime());
                wxCommentListDataVo.setPicList(marketComment.getPicUrls());
                wxCommentListDataVo.setAdminContent(marketComment.getAdminContent());
                wxCommentListDataVo.setContent(marketComment.getContent());
                listDataVos.add(wxCommentListDataVo);
                temp++;
                }


        }
        WxCommentListVo wxCommentListVo = new WxCommentListVo();
        wxCommentListVo.setLimit(limit);
        wxCommentListVo.setList(listDataVos);
        wxCommentListVo.setPage(page);
        wxCommentListVo.setPages(pages);
        wxCommentListVo.setTotal(Math.toIntExact(total));
        return wxCommentListVo;
    }
}

